package models

import (
	"time"

	"gorm.io/gorm"
)

type Permission struct {
	ID          uint      `json:"id" gorm:"primaryKey"`
	Name        string    `json:"name" gorm:"type:varchar(50);uniqueIndex;not null"`
	DisplayName string    `json:"display_name" gorm:"type:varchar(100);not null"`
	Description string    `json:"description" gorm:"type:text"`
	Resource    string    `json:"resource" gorm:"type:varchar(50);not null"` // 资源名称，如 user, role, permission
	Action      string    `json:"action" gorm:"type:varchar(50);not null"`   // 操作名称，如 create, read, update, delete
	IsActive    bool      `json:"is_active" gorm:"default:true"`
	CreatedAt   time.Time `json:"created_at"`
	UpdatedAt   time.Time `json:"updated_at"`
	DeletedAt   gorm.DeletedAt `json:"-" gorm:"index"`

	// 关联关系
	RolePermissions []RolePermission `json:"role_permissions" gorm:"foreignKey:PermissionID"`
	Roles           []Role           `json:"roles" gorm:"many2many:role_permissions;"`
}

// TableName 指定表名
func (Permission) TableName() string {
	return "permissions"
}